package com.jastar.android.jigsaw;

import android.graphics.Point;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.ViewGroup;
import android.widget.Chronometer;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.jastar.android.jigsaw.item.JigsawImage;
import com.jastar.android.jigsaw.util.Utils;

public class MainActivity extends AppCompatActivity {
    public static final String SIGN_LEVEL = "level";

    private JigsawView jv_image;
    private Chronometer timer;
    private TextView tv_status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        int level = getIntent().getIntExtra(SIGN_LEVEL, 0);

        timer = findViewById(R.id.timer);
        jv_image = findViewById(R.id.jv_image);
        tv_status = findViewById(R.id.tv_status);
        jv_image.setFocusable(true);
        jv_image.setFinishHandler(this::onFinish);

        reSizeJigsawView();
        tv_status.setText("");
        jv_image.initImage(JigsawImage.getImage(level));
        jv_image.requestFocus();
        timer.setBase(SystemClock.elapsedRealtime());
        timer.start();
    }

    private void reSizeJigsawView() {
        Point point = Utils.getActivityDisplaySize(this);
        ViewGroup.LayoutParams params = jv_image.getLayoutParams();
        params.width = point.x * 3 / 4;
        params.height = point.y * 3 / 4;
        jv_image.setLayoutParams(params);
    }

    private void onFinish() {
        timer.stop();
        tv_status.setText("恭喜完成!");
    }

}